package com.example.controller;

import com.example.comm.Result;

import com.example.controller.request.PeriodicalsRequest;

import com.example.controller.request.PredictRequest;
import com.example.dao.PeriodicalsDao;
import com.example.dao.PeriodicalsSpecification;

import com.example.entity.Periodicals;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

@RestController
@RequestMapping("/periodicals")
public class PeriodicalsController {
    @Autowired
    private PeriodicalsDao periodicalsDao;


    @PostMapping("/page")
    public Result page(@RequestBody PeriodicalsRequest periodicalsRequest) {
        Pageable pageable = PageRequest.of(periodicalsRequest.getPageNum() - 1, periodicalsRequest.getPageSize(), Sort.by(Sort.Direction.DESC, "id"));
//        System.out.println(periodicalsRequest.getCategories());
        Specification<Periodicals> specification = new PeriodicalsSpecification(periodicalsRequest);
        Page<Periodicals> page = periodicalsDao.findAll(specification, pageable);
        return Result.success(page);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        periodicalsDao.deleteById(id);
        return Result.success();
    }

    @GetMapping("/findAll")
    public Result findAll() {
        return Result.success(periodicalsDao.findAll());
    }

    @PostMapping("/predict")
    public Result predict(@RequestBody PredictRequest predictRequest) throws InterruptedException {
        Map<String, String> data = new HashMap<>();
        data.put("year", predictRequest.getYear());
        data.put("model_type", predictRequest.getModel_type());
        data.put("periodical",predictRequest.getPeriodical());
        CountDownLatch latch = new CountDownLatch(1);
        AtomicReference<String> result = new AtomicReference<>();
        WebClient.create()
                .post()
                .uri("http://127.0.0.1:5000/predict")
                .body(BodyInserters.fromValue(data))
                .retrieve()
                .bodyToMono(String.class)
                // 订阅响应结果
                .subscribe(response -> {
                    // 在这里处理响应结果
                    result.set(response);
                    // 递减计数器
                    latch.countDown();
                });
        // 等待计数器归零
        latch.await();
        System.out.println(result.get());
        return Result.success(result.get());
    }


}
